/*------------------------------+
|	  Particle System Class		|
|	--------------------------	|
|	Manager that loads emitters,|
|	renders and updates them,	|
|	and controls their memory	|
+------------------------------*/

#include "ParticleSystem.h"
#include "Emitter.h"

ParticleSystem::ParticleSystem() {
	
}

ParticleSystem::~ParticleSystem()
{

}

// Create a template emitter from a file. Returns a pointer to that template
Emitter* ParticleSystem::LoadEmitterTemplate(std::string file)
{
	Emitter* e;
	return e;
}

// Create an actual emitter for use in game
Emitter* ParticleSystem::CreateEmitter(Emitter* master)
{
	Emitter* e = new Emitter(*master);
	Emitters.push_back(e);
	return e;
}

// Destroy an emitter
void ParticleSystem::DestroyEmitter(Emitter* emitter)
{
	for( int i = 0; i < Emitters.size(); ++i )
	{
		if( emitter == Emitters[i] )
		{
			delete emitter;
			Emitters.erase(Emitters.begin() + i);
			break;
		}
	}
	Emitters.shrink_to_fit();
}

// Activate an emitter
void ParticleSystem::ActivateEmitter(Emitter* emitter, bool looping)
{
	if( looping )
		emitter->Loop();
	else
		emitter->Burst();
}

// Stop a looping emitter
void ParticleSystem::DeactivateEmitter(Emitter* emitter)
{
	emitter->Loop(false);
}

// Return an instance of the manager
ParticleSystem* ParticleSystem::GetInstance()
{
	static ParticleSystem instance;
	return &instance;
}

// Update all emitters
void ParticleSystem::Update(float elapsedTime)
{
	for( int i = 0; i < Emitters.size(); ++i )
		Emitters[i]->Update(elapsedTime);
}

// Render all emitters
void ParticleSystem::Render(void)
{
	for( int i = 0; i < Emitters.size(); ++i )
		Emitters[i]->Render();
}

// Shutdown the System
void ParticleSystem::Shutdown(void)
{
	// Empty the physical emitters
	for( int i = 0; i < Emitters.size(); ++i )
	{
		Emitters[i]->Shutdown();
		delete Emitters[i];
		Emitters.erase(Emitters.begin() + i);
	}

	// Empty the templates
	for( int i = 0; i < Templates.size(); ++i )
	{
		Templates[i]->Shutdown();
		delete Templates[i];
		Templates.erase(Templates.begin() + i);
	}
}